<template>
	<view>
		<u-popup :show="loginPopupShow" mode="bottom" :round="10" @close="closeLogin" zIndex="999998"
			:closeOnClickOverlay="false" :closeable="true">
			<view class="loginPopup">
				<!-- <view class="title">
                    <text class="text">登录</text>
                    <u-icon name="close" color="#999" size="16" @click='closeLogin'></u-icon>
                </view> -->

				<!-- <image src="@/static/images/logo1.jpg" mode="widthFix" class="logo"></image> -->

				<view class="welcome">小程序</view>

				<u-button type="success" icon="weixin-fill" shape="circle" open-type="getPhoneNumber"
					@getphonenumber="getPhoneNumber" text="微信一键登录"></u-button>

				<view class="privacy">
					授权登录即表示已读并同意 <text @click="clickPrivacy">《隐私政策》</text>
				</view>
			</view>
		</u-popup>


		<!-- <u-popup :show="privacyShow" @close="closePrivacy" mode="bottom" :round="10" zIndex="9999999" :closeable="true">
			<view class="privacyPop">
				<scroll-view class="scrollView" scroll-x="false" scroll-y="true">

					<u-parse :content="privacyInfo"></u-parse>
				</scroll-view>
			</view>
		</u-popup> -->


	</view>
</template>
<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				privacyShow: false,
				loginCode: ''
			}
		},
		watch: {
			loginPopupShow(val) {
				// 静默登录
				this.getCode()
			}
		},
		computed: {
			...mapState(['loginPopupShow']),
		},
		methods: {
			...mapMutations(['setLoginPopupShow']),

			closeLogin() {
				this.setLoginPopupShow(false);
			},
			// 获取code
			getCode() {
				var _this = this;
				uni.login({
					provider: 'weixin',
					success(res) {
						console.log(res, "获取code")
						_this.loginCode = res.code
					}
				})
			},
			// 静默登录：就是已经登录过的用户，被迫下线。调用此接口快速登录
			getLoginByUnion() {
				console.log("自动静默登录--自定义接口--")
				var _this = this;
				uni.login({
					success(res) {
						_this.$post({
							method: "member.login",
							code: res.code,
						}).then(re => {
							let httpData = {
								token: re.data.token
							}
							_this.$store.commit('setUserInfo', httpData)
						}).catch(() => {
							// 自动静默登录失败--打开手动登录
							that.isLoading = false
						})
					}
				})
			},
			getPhoneNumber(e) {
				console.log(e, '===getPhoneNumber')
				var _this = this;
				if (e.detail.errMsg == 'getPhoneNumber:ok') {
					_this.$post({
						encryptedData: e.detail.encryptedData,
						iv: e.detail.iv,
						code: _this.loginCode,
					}).then(re => {
						if (re.code == 200) {
							_this.$store.dispatch("UserInfo");
							_this.setLoginPopupShow(false);
							uni.reLaunch({
								url: '/pages/index/index'
							})
						}
						uni.showToast({
							title: re.msg,
							icon: 'none'
						});

					})
				} else {
					_this.setLoginPopupShow(false);
					uni.showToast({
						title: '您拒绝了手机号授权',
						icon: 'none'
					});
				}
			},
			closePrivacy() {
				this.privacyShow = false
			},
			clickPrivacy() {
				this.privacyShow = true
			}
		},
	}
</script>
<style lang="scss">
	.loginPopup {
		padding: 50rpx 30rpx;

		.title {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.text {
				font-size: 26rpx;

			}
		}

		.logo {
			display: block;
			margin: auto;
			width: 200rpx;
			height: 200rpx;
		}

		.welcome {
			text-align: center;
			font-size: 28rpx;
			font-weight: bold;
			margin: 20rpx 0 70rpx;
		}

		.privacy {
			font-size: 26rpx;
			text-align: center;
			margin-top: 30rpx;
			color: #999;

			text {
				color: #1e95ee;
				font-size: 26rpx;
			}
		}
	}

	.privacyPop {
		padding: 80rpx 30rpx 30rpx;

		.title {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.text {
				font-size: 26rpx;

			}
		}

		.scrollView {
			min-height: 40vh;
			max-height: 77vh;
		}
	}
</style>